package com.it.controller;

import com.it.pojo.Menu;
import com.it.pojo.Role;
import com.it.pojo.User;
import com.it.service.MenuService;
import com.it.service.RoleService;
import com.it.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: zgc
 * @version: 1.0
 * @description:
 * @date: 2023/12/14 10:04
 */
@Controller
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private UserService userService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private RoleService roleService;

    /*登录*/
    @PostMapping("/in")
    public ModelAndView loginIn(String username, String password, HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        User user = userService.findNameAndPwd(username, password);
        if (user==null){
            modelAndView.addObject("error","用户名或密码错误");
            modelAndView.setViewName("/pages/login/login");
        }else{
            if (user.getUserStatus()==0){
                List<Menu> list = menuService.findByUserId(user.getUserId());
                List<String> list2 = new ArrayList<>();
                for (Menu menu : list) {
                    list2.add(menu.getMenuId());
                }
                List<Menu> parentId = menuService.findByParentId(list2);
                Role role = roleService.findById(user.getRoleId());
                session.setAttribute("role",role);
                session.setAttribute("user",user);
                session.setAttribute("menuList",list);
                session.setAttribute("menuList2",parentId);
                modelAndView.setViewName("index");
            }else{
                //该用户以被禁用
                modelAndView.addObject("error","该用户已被禁用");
                modelAndView.setViewName("/pages/login/login");
            }
        }
        return modelAndView;
    }

    /*修改密码*/
    @PostMapping("/updatePwd")
    public String updatePwd(String userId,String verifyPassword){
        User user = new User();
        user.setUserId(userId);
        user.setUserPwd(verifyPassword);
        userService.updateUser(user);
        return "/pages/login/login";
    }

    /*注册用户*/
    @GetMapping("/register")
    public ModelAndView register(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/pages/login/register");
        List<Role> list = roleService.findAll();
        modelAndView.addObject("roleList",list);
        return modelAndView;
    }

    /*创建用户*/
    @PostMapping("/createUser")
    public ModelAndView createUser(User user){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/pages/login/register");
        User user1 = userService.findByName(user.getUserLoginname());
        if (user1==null){
            userService.insertUser(user);
        }else{
            modelAndView.addObject("error","用户名已存在");
        }
        return modelAndView;
    }



}
